package com.ruoyi.project.paea.signature.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.CommonParaTool;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.paea.signature.domain.PaeaParaAuthorizedSignature;
import com.ruoyi.project.paea.signature.mapper.PaeaParaAuthorizedSignatureMapper;
import com.ruoyi.project.paea.signature.service.IPaeaParaAuthorizedSignatureService;
import com.ruoyi.project.paea.signaturelog.domain.PaeaParaRizedSignatureLog;
import com.ruoyi.project.paea.signaturelog.mapper.PaeaParaRizedSignatureLogMapper;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.mapper.DeptMapper;
import com.ruoyi.project.tool.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 有权人签字管理Service业务层处理
 *
 * @author shangkun.cui
 * @date 2020-04-22
 */
@Service
public class PaeaParaAuthorizedSignatureServiceImpl extends ServiceImpl<PaeaParaAuthorizedSignatureMapper, PaeaParaAuthorizedSignature> implements IPaeaParaAuthorizedSignatureService
{
    @Autowired
    private PaeaParaAuthorizedSignatureMapper paeaParaAuthorizedSignatureMapper;
    @Autowired
    private PaeaParaRizedSignatureLogMapper paeaParaRizedSignatureLogMapper;
    @Autowired
    private DeptMapper deptMapper;
    /**
     * 查询有权人签字管理
     *
     * @param id 有权人签字管理ID
     * @return 有权人签字管理
     */
    @Override
    public PaeaParaAuthorizedSignature selectPaeaParaAuthorizedSignatureById(String id)
    {
        return paeaParaAuthorizedSignatureMapper.selectPaeaParaAuthorizedSignatureById(id);
    }

    /**
     * 查询有权人签字管理列表
     *
     * @param paeaParaAuthorizedSignature 有权人签字管理
     * @return 有权人签字管理
     */
    @Override
    public List<PaeaParaAuthorizedSignature> selectPaeaParaAuthorizedSignatureList(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature)
    {
        return paeaParaAuthorizedSignatureMapper.selectPaeaParaAuthorizedSignatureList(paeaParaAuthorizedSignature);
    }

    /**
     * 新增有权人签字管理
     *
     * @param paeaParaAuthorizedSignature 有权人签字管理
     * @return 结果
     */
    @Override
    public int insertPaeaParaAuthorizedSignature(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature)
    {
        paeaParaAuthorizedSignature.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        paeaParaAuthorizedSignature.setCreateTime(DateUtils.getNowDate());
        return paeaParaAuthorizedSignatureMapper.insertPaeaParaAuthorizedSignature(paeaParaAuthorizedSignature);
    }

    /**
     * 修改有权人签字管理
     *
     * @param paeaParaAuthorizedSignature 有权人签字管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePaeaParaAuthorizedSignature(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature)
    {
        String changeContent = this.getChangeContent(paeaParaAuthorizedSignature);
        paeaParaAuthorizedSignature.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        paeaParaAuthorizedSignature.setUpdateTime(DateUtils.getNowDate());
        paeaParaAuthorizedSignatureMapper.updatePaeaParaAuthorizedSignature(paeaParaAuthorizedSignature);
        String operateContentType = "修改";
        if(paeaParaAuthorizedSignature.getStatus()==1){
            operateContentType = "新增修改";
        }else if(paeaParaAuthorizedSignature.getStatus()==0){
            operateContentType = "退回修改";
        }
        PaeaParaRizedSignatureLog signaturelog = this.createSignaturelog(paeaParaAuthorizedSignature,operateContentType);
        signaturelog.setChangeContent(changeContent);
        paeaParaRizedSignatureLogMapper.insertPaeaParaRizedSignatureLog(signaturelog);
        return 1;
    }

    /**
     * 删除有权人签字管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaeaParaAuthorizedSignatureByIds(String ids)
    {
        return paeaParaAuthorizedSignatureMapper.deletePaeaParaAuthorizedSignatureByIds(Convert.toStrArray(ids));
    }


    /**
     * 删除有权人签字管理信息
     *
     * @param id 有权人签字管理ID
     * @return 结果
     */
    @Override
    public int deletePaeaParaAuthorizedSignatureById(String id)
    {
        return paeaParaAuthorizedSignatureMapper.deletePaeaParaAuthorizedSignatureById(id);
    }

    @Override
    @Transactional
    public int saveSingature(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature, MultipartFile file) {
        List<PaeaParaAuthorizedSignature> signaturesList = paeaParaAuthorizedSignatureMapper.selectPaeaParaAuthorizedSignatureList(paeaParaAuthorizedSignature);
        int i = signaturesList.size();
        String orgCode = paeaParaAuthorizedSignature.getOrgCode();
        String dateString = DateTimeUtil.getYYMMDDHHMMSSDate();
        MultipartFile imgFile = file;
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件上传路径
        String filePath = RuoYiConfig.getUploadPath()+"/signature/";
        // 解决中文问题,liunx 下中文路径,图片显示问题
        fileName = dateString + orgCode + i + suffixName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String singaturePath = filePath+fileName;
        paeaParaAuthorizedSignature.setId(UUID.randomUUID().toString().replaceAll("-",""));
        paeaParaAuthorizedSignature.setSignatureName(fileName);
        paeaParaAuthorizedSignature.setSignaturePath(singaturePath);
        paeaParaAuthorizedSignature.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        paeaParaAuthorizedSignature.setCreateTime(DateUtils.getNowDate());
        paeaParaAuthorizedSignature.setStatus(new Integer(1));//待用
        paeaParaAuthorizedSignatureMapper.insertPaeaParaAuthorizedSignature(paeaParaAuthorizedSignature);
        PaeaParaRizedSignatureLog signaturelog = this.createSignaturelog(paeaParaAuthorizedSignature,"新增");
        paeaParaRizedSignatureLogMapper.insertPaeaParaRizedSignatureLog(signaturelog);
        return 1;
    }

    @Override
    @Transactional
    public int updateSingature(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature, MultipartFile file) {
        List<PaeaParaAuthorizedSignature> signaturesList = paeaParaAuthorizedSignatureMapper.selectPaeaParaAuthorizedSignatureList(paeaParaAuthorizedSignature);
        int i = signaturesList.size();
        String orgCode = paeaParaAuthorizedSignature.getOrgCode();
        String dateString = DateTimeUtil.getYYMMDDHHMMSSDate();
        if(file!=null){
            MultipartFile imgFile = file;
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            // 文件上传路径
            String filePath = RuoYiConfig.getUploadPath()+"/signature/";
            // 解决中文问题,liunx 下中文路径,图片显示问题
            fileName = dateString + orgCode + i + suffixName;
            File dest = new File(filePath + fileName);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //删除之前的图片
            removeImg(paeaParaAuthorizedSignature.getSignaturePath());
            String singaturePath = filePath+fileName;
            paeaParaAuthorizedSignature.setSignatureName(fileName);
            paeaParaAuthorizedSignature.setSignaturePath(singaturePath);
        }
        paeaParaAuthorizedSignature.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        paeaParaAuthorizedSignature.setUpdateTime(DateUtils.getNowDate());
        String operateContentType = "修改";
        if(paeaParaAuthorizedSignature.getStatus()==1){
            operateContentType = "新增修改";
        }else if(paeaParaAuthorizedSignature.getStatus()==0){
            operateContentType = "退回修改";
        }
        paeaParaAuthorizedSignature.setStatus(new Integer(1));//待用
        String changeContent = this.getChangeContent(paeaParaAuthorizedSignature);
        paeaParaAuthorizedSignatureMapper.updatePaeaParaAuthorizedSignature(paeaParaAuthorizedSignature);
        PaeaParaRizedSignatureLog signaturelog = this.createSignaturelog(paeaParaAuthorizedSignature,operateContentType);
        signaturelog.setChangeContent(changeContent);
        paeaParaRizedSignatureLogMapper.insertPaeaParaRizedSignatureLog(signaturelog);
        return 1;

    }
    @Override
    @Transactional
    public AjaxResult approSave(String id, String approRemark, String flag) {
        PaeaParaAuthorizedSignature paeaParaAuthorizedSignature = paeaParaAuthorizedSignatureMapper.selectPaeaParaAuthorizedSignatureById(id);
        paeaParaAuthorizedSignature.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        paeaParaAuthorizedSignature.setUpdateTime(DateUtils.getNowDate());
        String operateContentType = "审批通过";
        String returnStr = "审批通过，有权人生效";
        paeaParaAuthorizedSignature.setStatus(new Integer(2));//通过
        if(StringUtils.equals("back",flag)){
            operateContentType = "审批退回";
            returnStr = "已退回，有权人未生效";
            paeaParaAuthorizedSignature.setStatus(new Integer(0));//退回
        }
        paeaParaAuthorizedSignature.setApproRemark(approRemark);
        paeaParaAuthorizedSignatureMapper.updatePaeaParaAuthorizedSignature(paeaParaAuthorizedSignature);
        PaeaParaRizedSignatureLog signaturelog = this.createSignaturelog(paeaParaAuthorizedSignature,operateContentType);
        paeaParaRizedSignatureLogMapper.insertPaeaParaRizedSignatureLog(signaturelog);
        return AjaxResult.success(returnStr);
    }
    @Override
    @Transactional
    public AjaxResult signatureStop(String id) {
        PaeaParaAuthorizedSignature paeaParaAuthorizedSignature = paeaParaAuthorizedSignatureMapper.selectPaeaParaAuthorizedSignatureById(id);
        paeaParaAuthorizedSignature.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        paeaParaAuthorizedSignature.setUpdateTime(DateUtils.getNowDate());
        String operateContentType = "停用";
        paeaParaAuthorizedSignature.setStatus(new Integer(3));//停用
        paeaParaAuthorizedSignatureMapper.updatePaeaParaAuthorizedSignature(paeaParaAuthorizedSignature);
        PaeaParaRizedSignatureLog signaturelog = this.createSignaturelog(paeaParaAuthorizedSignature,operateContentType);
        paeaParaRizedSignatureLogMapper.insertPaeaParaRizedSignatureLog(signaturelog);
        return AjaxResult.success("已停用");
    }
    private PaeaParaRizedSignatureLog createSignaturelog(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature,String operateContentType){
        PaeaParaRizedSignatureLog signatureLog = new PaeaParaRizedSignatureLog();
        signatureLog.setId(UUID.randomUUID().toString().replaceAll("-",""));
        signatureLog.setSignatureId(paeaParaAuthorizedSignature.getId());
        signatureLog.setOperateBy(ShiroUtils.getSysUser().getLoginName());
        signatureLog.setOperateName(ShiroUtils.getSysUser().getUserName());
        signatureLog.setOperateTime(DateUtils.getNowDate());
        signatureLog.setOperateContent(operateContentType);
        if(paeaParaAuthorizedSignature.getStatus()==2||paeaParaAuthorizedSignature.getStatus()==0){
            signatureLog.setOperateRemark(paeaParaAuthorizedSignature.getApproRemark());
        }else if(paeaParaAuthorizedSignature.getStatus()!=3){
            signatureLog.setOperateRemark(paeaParaAuthorizedSignature.getRemark());
        }
        signatureLog.setOrgCode(ShiroUtils.getSysUser().getDeptId());
        Dept dept = deptMapper.selectDeptById(ShiroUtils.getSysUser().getDeptId());
        String deptName = dept!=null?dept.getDeptName():"";
        signatureLog.setOrgName(deptName);
        signatureLog.setStatus(new Integer(0));
        return signatureLog;
    }
    private String getChangeContent(PaeaParaAuthorizedSignature paeaParaAuthorizedSignature){
        if(paeaParaAuthorizedSignature.getId()==null){
            return "";
        }
        PaeaParaAuthorizedSignature signature = paeaParaAuthorizedSignatureMapper.selectPaeaParaAuthorizedSignatureById(paeaParaAuthorizedSignature.getId());
        String content = "";
        if(signature==null){
            return content;
        }
        if(!StringUtils.equals(paeaParaAuthorizedSignature.getOrgCode(),signature.getOrgCode())){
            content += "原机构号:"+signature.getOrgCode()+",现机构号:"+paeaParaAuthorizedSignature.getOrgCode()+";";
        }
        if(!StringUtils.equals(paeaParaAuthorizedSignature.getOrgName(),signature.getOrgName())){
            content += "原机构名:"+signature.getOrgName()+",现机构名:"+paeaParaAuthorizedSignature.getOrgName()+";";
        }
        if(!StringUtils.equals(paeaParaAuthorizedSignature.getPosition(),signature.getPosition())){
            content += "原授权人职位:"+signature.getPosition()+",现授权人职位:"+paeaParaAuthorizedSignature.getPosition()+";";
        }
        if(!StringUtils.equals(paeaParaAuthorizedSignature.getName(),signature.getName())){
            content += "原授权人姓名:"+signature.getName()+",现授权人姓名:"+paeaParaAuthorizedSignature.getName()+";";
        }
        if(!StringUtils.equals(paeaParaAuthorizedSignature.getSignaturePath(),signature.getSignaturePath())){
            content += "变更授权人签字图片;";
        }
        return content;
    }
    @Override
    public void removeImg(List<PaeaParaAuthorizedSignature> list) {
        for (int i=0;i<=list.size()-1;i++){
            PaeaParaAuthorizedSignature signature = list.get(i);
            if(StringUtils.isEmpty(signature.getSignaturePath())){
                return;
            }
            File file = new File(signature.getSignaturePath());
            if (file.exists()){
                if (file.isFile()){
                    file.delete();
                }
            }
        }
    }
    //删除单张
    private void removeImg(String path) {
            if(StringUtils.isEmpty(path)){
                return;
            }
            File file = new File(path);
            if (file.exists()){
                if (file.isFile()){
                    file.delete();
                }
            }
    }

}
